
Imports System.Net
Imports System.IO
Imports System.Drawing


' This Module contains the Structures, Helper Functions and Exceptions that are used by all the providers

Public Module GlobalDefnsAndFunctions
    Public Structure StockQuoteData
        Dim Symbol As String
        Dim StockRate As Decimal
        Dim StockChangeValue As Decimal
        Dim StockChangePercent As Decimal
        Dim StockChangeDownwards As Boolean
        Dim DateOfTrade As DateTime
    End Structure

    'Enum Holding the Exchange from which to get the Quote
    Public Enum IndiaExchange
        SpecifiedInSymbol
        BSE
        NSE
    End Enum
    'Enum of the Duration Type for Charts
    Public Enum ChartDurationType
        OneDay
        ThreeMonth
        SixMonth
        OneYear
        TwoYear
        FiveYear
    End Enum

    'Exception Class Thrown when a wrong Symbol is passed
    <Serializable()> _
    Public Class InvalidSymbolException
        Inherits Exception


        Public Overrides ReadOnly Property Message() As String
            Get
                Return String.Format("The Symbol ""{0}"" was Invalid", Me.Data("Symbol"))
            End Get
        End Property
    End Class

    'Exception Class. Thrown when we are unable to get the Quote Info due to a WebClient Error
    Public Class DataConnectionException
        Inherits System.Net.WebException

        Public Overrides ReadOnly Property Message() As String
            Get
                Return "There was an error getting the Quote from the Website. This may be because you are not connected to the Internet, or the site is down"
            End Get
        End Property
    
    End Class
    'Given a URL returns the data as a String. Used when returning HTML
    Public Function GetURLString(ByVal URL As String) As String
        Dim req As WebClient
        Dim PageData As String
        req = New WebClient()

        Try
            PageData = req.DownloadString(URL)
        Catch ex As Exception
            Throw New DataConnectionException
        Finally
            req.Dispose()
        End Try

        Return PageData
    End Function

    'Given a URL returns a Byte Array. Used for Images currently
    Public Function GetURLData(ByVal URL As String) As Byte()
        Dim req As WebClient
        Dim PageData As Byte()
        req = New WebClient()

        Try
            PageData = req.DownloadData(URL)
        Catch ex As Exception
            Throw New DataConnectionException
        Finally
            req.Dispose()
        End Try

        Return PageData
    End Function

    'Helper Function that helps Convert a BypeArray to an Image Type. Currently used for handling Cha
    Public Function ByteArrayToImage(ByVal byteArrayIn As Byte()) As Image
        Dim ms As New MemoryStream(byteArrayIn)
        Dim RetImage As Image = Image.FromStream(ms)
        Return RetImage
    End Function
End Module
